package com.exam.service.impl;

import com.exam.dao.ExamDao;
import com.exam.dao.QuestionDao;
import com.exam.dao.RecordDao;
import com.exam.dao.TeacherDao;
import com.exam.domain.entity.Exam;
import com.exam.domain.entity.Question;
import com.exam.domain.entity.Record;
import com.exam.domain.entity.Teacher;
import com.exam.service.TeacherService;
import com.exam.util.BaseResult;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TeacherServiceImpl implements TeacherService {

    private final TeacherDao teacherDao;
    private final RecordDao recordDao;


    public TeacherServiceImpl() {
        teacherDao = new TeacherDao();
        recordDao = new RecordDao();

    }

    @Override
    public BaseResult<String> addQuestion(Integer teacherId, String title, String answer, String optionA, String optionB, String optionC, String optionD) {
        Integer result = teacherDao.addQuestion(teacherId,title,answer,optionA,optionB,optionC,optionD);

        return result>0?BaseResult.success("添加试题成功"):BaseResult.error("添加失败");
    }

    @Override
    public BaseResult<List<Question>> getAllQuestionsByTeacherId(Integer teacherId) {
        List<Question> questions = teacherDao.getAllQuestionsByTeacherId(teacherId);
        return BaseResult.success(questions);
    }

    @Override
    public BaseResult<String> createExam(Integer teacherId, String examTitle, int questionCount, int questionValue) {
        // 创建之前先判断，数据库总共的题数
        List<Question> questions = teacherDao.getAllQuestionsByTeacherId(teacherId);
        if (questions.size()<questionCount) {
            return BaseResult.error("您先前创建的题目数量："+questions.size()+"不足以生成题数为："+questionCount+"的试卷");
        }
        // 随机打乱题目列表
        Collections.shuffle(questions);
        // 选取前 questionCount 道题目
        List<Question> selectedQuestions = questions.subList(0, questionCount);
        // 封装题目 ID 到 List<Integer>
        List<Integer> questionIds = new ArrayList<>();
        for (Question question : selectedQuestions) {
            questionIds.add(question.getQuestionId());
        }
        // 题目数量再合理范围，使用随机算法，生成一张试卷
        teacherDao.createExamWithQuestions(teacherId,examTitle,questionCount,questionValue,questionIds);
        // 插入exam_question表相应的题目数量
        // 下面也是一条涉及数据库操作的，怎么保证，上面两条数据库操作的原子性呢
        return BaseResult.success("创建成功！");
    }

    @Override
    public BaseResult<List<Exam>> getExams(Integer teacherId) {
        List<Exam> exams = teacherDao.getExamsByTeacherId(teacherId);
        return BaseResult.success(exams);
    }

    @Override
    public BaseResult<List<Question>> getQuestionsByQuestionText(Integer teacherId, String questionText) {
        // 通过模糊查询问题列表
        List<Question> questions = teacherDao.getTeacherQuestionsByQuestionText(teacherId,questionText);
        return BaseResult.success(questions);
    }

    @Override
    public BaseResult<Teacher> getTeacherInfo(Integer teacherId) {
        Teacher teacher = teacherDao.getTeacherInfoById(teacherId);

        return BaseResult.success(teacher);
    }

    @Override
    public BaseResult<List<Record>> getStudentsExamRecord(Integer examId) {
        // 查询学生的考试记录
        List<Record> records = recordDao.getExamRecordListByExamId(examId);
        return BaseResult.success(records);
    }
}
